package at.ac.univie.zsu.aguataplan.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import at.ac.univie.zsu.aguataplan.exception.DatabaseException;
import at.ac.univie.zsu.aguataplan.exception.ExportException;
import at.ac.univie.zsu.aguataplan.property.GeneralProperties;
import at.ac.univie.zsu.aguataplan.property.I18nProperties;
import at.ac.univie.zsu.aguataplan.service.ExportManager;

/**
 * @author gerry
 * 
 */
public class DialogExport extends JDialog implements UIConstants {

	private static final long serialVersionUID = 1L;

	private JFrame parent;
	private GeneralProperties generalProperties;
	private I18nProperties i18nProp;

	private ExportManager exportManager;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public DialogExport(MainWindow parent, GeneralProperties generalProperties) {
		super(parent, true);

		this.parent = parent;
		this.generalProperties = generalProperties;
		this.i18nProp = generalProperties.getI18nProperties();
		this.exportManager = parent.getServices().getExportManager();

		this.setLayout(null);
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setLocationByPlatform(true);
		setSize(600, 500);
		setTitle(i18nProp.getString(EXPORT_HEADER));
		setResizable(false);

		addEntries();

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	private void addEntries() {

		JButton exportDetectedEventBtn = new JButton(i18nProp.getString(EXPORT_BUTTON_DETECTEDEVENT));
		exportDetectedEventBtn.setSize(BUTTON_SIZE_LARGE_X, BUTTON_SIZE_Y);
		exportDetectedEventBtn.setLocation(SPACING, SPACING);

		exportDetectedEventBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					exportManager.exportDetectedEvent();
					JOptionPane.showMessageDialog(DialogExport.this,
							i18nProp.getString(EXPORT_DETECTEDEVENT_DONE_DESC),
							i18nProp.getString(EXPORT_DETECTEDEVENT_DONE), JOptionPane.INFORMATION_MESSAGE);
				} catch (DatabaseException e1) {
					JOptionPane.showMessageDialog(DialogExport.this, i18nProp.getString(EX_EXPORT_DETECTEDEVENT_DESC),
							i18nProp.getString(EX_EXPORT), JOptionPane.ERROR_MESSAGE);
				} catch (ExportException e2) {
					JOptionPane.showMessageDialog(DialogExport.this, i18nProp.getString(EX_EXPORT_DETECTEDEVENT_DESC),
							i18nProp.getString(EX_EXPORT), JOptionPane.ERROR_MESSAGE);
				}
			}
		});

		JButton exportEventBtn = new JButton(i18nProp.getString(EXPORT_BUTTON_EVENT));
		exportEventBtn.setSize(BUTTON_SIZE_LARGE_X, BUTTON_SIZE_Y);
		exportEventBtn.setLocation(SPACING, SPACING + BUTTON_SIZE_Y + SPACING);

		exportEventBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					exportManager.exportEvent();
					JOptionPane.showMessageDialog(DialogExport.this, i18nProp.getString(EXPORT_EVENT_DONE_DESC),
							i18nProp.getString(EXPORT_EVENT_DONE), JOptionPane.INFORMATION_MESSAGE);
				} catch (DatabaseException e1) {
					JOptionPane.showMessageDialog(DialogExport.this, i18nProp.getString(EX_EXPORT_EVENT_DESC),
							i18nProp.getString(EX_EXPORT), JOptionPane.ERROR_MESSAGE);
				} catch (ExportException e2) {
					JOptionPane.showMessageDialog(DialogExport.this, i18nProp.getString(EX_EXPORT_EVENT_DESC),
							i18nProp.getString(EX_EXPORT), JOptionPane.ERROR_MESSAGE);
				}
			}

		});
		this.add(exportDetectedEventBtn);
		this.add(exportEventBtn);
	}
}
